package cn.lanchengkeji.datafillingsystem.controller;




import cn.lanchengkeji.datafillingsystem.entity.Menu;
import cn.lanchengkeji.datafillingsystem.entity.OperationLog;
import cn.lanchengkeji.datafillingsystem.entity.Users;
import cn.lanchengkeji.datafillingsystem.service.IMenuService;
import cn.lanchengkeji.datafillingsystem.service.IOperationLogService;
import cn.lanchengkeji.datafillingsystem.service.IUsersService;
import cn.lanchengkeji.datafillingsystem.utils.HttpUtils;
import cn.lanchengkeji.datafillingsystem.utils.excel.ExcelUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 过电流
 * </p>
 *
 * @author yixu
 * @since 2018-12-22
 */
public class BaseController {

    @Resource
    IUsersService usersService;

    @Resource
    IOperationLogService iOperationLogService;

    @Resource
    IMenuService iMenuService;

    protected Users getLoginUser() {
        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        Users users = usersService.getUserByName(String.valueOf(obj));
        return users;
    }

    protected void exportExcel(HttpServletResponse response, List list) {
        response.setContentType("octets/stream");
        String excelName = "导出";
        try {
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(excelName, "utf-8") + ".xlsx");
            OutputStream out = response.getOutputStream();
            ExcelUtils.writeExcelToStream(out, list, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void recordOperationLog(HttpServletRequest request, String operation, Integer userId, String operationType) {
        String path = request.getParameter("recordOperationLogMenuId");
        Users users = usersService.getById(userId);
        OperationLog operationLog = new OperationLog();
        operationLog.setOperationType(operationType);
        operationLog.setOperationUserId(users.getId());
        operationLog.setOperationRealName(users.getRealName());
        operationLog.setOperationUserName(users.getUserName());
        operationLog.setOperationUserOrganization(users.getOrganization());
        operationLog.setOperationDate(new Date());
        operationLog.setRemoteIp(HttpUtils.getIPAddress(request));
        operationLog.setOperationEvent(operation);
        // 只有二级菜单下的操作才记录日志
        String[] split = path.split("/");
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Menu.PATH, split[split.length - 1]);
        Menu menu = iMenuService.getOne(queryWrapper);
        Menu pMenu = iMenuService.getById(menu.getPid());
        operationLog.setOperationMenu(pMenu.getTitle().concat("->").concat(menu.getTitle()));
        iOperationLogService.save(operationLog);
    }
}
